package com.ming.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ming.yygh.cmn.client.DictFeignClient;
import com.ming.yygh.hosp.repository.HospitalRepository;
import com.ming.yygh.hosp.service.HospitalService;
import com.ming.yygh.model.hosp.Hospital;
import com.ming.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Light
 * @date 2021/12/1 17:53
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public boolean save(Map<String, Object> paramMap) {
        // 把参数map集合转换为对象 Hospital
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        // 判断mongo数据库中是否已经有数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        // 如果存在，进行修改
        if (null != hospitalExist) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else { // 如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
        return true;
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        // 创建pageRequest对象
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        // 构建条件匹配器
        ExampleMatcher matcher = ExampleMatcher
                .matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        // HospitalQueryVo对象转换Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        // 创建example对象
        Example<Hospital> example = Example.of(hospital, matcher);
        // 调用方法实现查询
        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageRequest);
        List<Hospital> content = hospitalPage.getContent();
        content.forEach(item -> {
            // 根据dictCode和value获取医院等级名称
            String hosTypeString = dictFeignClient.getName("Hostype", item.getHostype()).getData();
            // 查询省 市 地区
            String provinceString = dictFeignClient.getName(item.getProvinceCode()).getData();
            String cityString = dictFeignClient.getName(item.getCityCode()).getData();
            String districtString = dictFeignClient.getName(item.getDistrictCode()).getData();

            item.getParam().put("hosTypeString", hosTypeString);
            item.getParam().put("fullAddress", provinceString + cityString + districtString);
        });

        return hospitalPage;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        // 根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        // 设置修改的值
        hospital.setStatus(status);
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospById(String id) {
        HashMap<String, Object> result = new HashMap<>();
        Hospital hospital = hospitalRepository.findById(id).get();
        // 医院等级查询
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype()).getData();
        hospital.getParam().put("hosTypeString", hostypeString);
        result.put("hospital", hospital);
        result.put("bookingRule", hospital.getBookingRule());
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (null != hospital) {
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.getHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> findHospDetail(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype()).getData();
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode()).getData();
        String cityString = dictFeignClient.getName(hospital.getCityCode()).getData();
        String districtString = dictFeignClient.getName(hospital.getDistrictCode()).getData();

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }

}
